/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package system.ai;

import com.aionemu.gameserver.ai.AIClasss;
import com.aionemu.gameserver.ai.CtrlIntention;
import com.aionemu.gameserver.geoEngine.GeoEngine;
import com.aionemu.gameserver.model.EmotionType;
import com.aionemu.gameserver.model.gameobjects.AionObject;
import com.aionemu.gameserver.model.gameobjects.Creature;
import com.aionemu.gameserver.model.gameobjects.Npc;
import com.aionemu.gameserver.model.gameobjects.VisibleObject;
import com.aionemu.gameserver.model.gameobjects.instance.Monster;
import com.aionemu.gameserver.model.gameobjects.state.CreatureState;
import com.aionemu.gameserver.model.templates.npcTemplate.CreatureTemplate;
import com.aionemu.gameserver.network.aion.serverpackets.SM_EMOTION;
import com.aionemu.gameserver.utils.MathUtil;
import com.aionemu.gameserver.utils.PacketSendUtility;
import com.aionemu.gameserver.world.World;

/**
 * @author Mr. Poke
 *
 */
public class GuardAI extends NpcAI
{

	/**
	 * @param owner
	 */
	public GuardAI(Creature owner)
	{
		super(owner);
	}


	/* (non-Javadoc)
	 * @see system.ai.NpcAI#thinkActive()
	 */
	@Override
	protected void thinkActive()
	{
		Creature mostHate = getOwner().getAggroList().getMostHated();
		if (mostHate != null && MathUtil.getDistance(owner, mostHate) < 30 && !mostHate.getLifeStats().isAlreadyDead())
		{
			setIntention(CtrlIntention.ATTACK, mostHate);
			return;
		}

		for (VisibleObject object : getOwner().getKnownList().getKnownObjects().values())
		{
			if (!(object instanceof Npc))
				continue;
			Npc npc = (Npc)object;
			if (!getOwner().isEnemy(npc) || !(npc.isInState(CreatureState.COMBAT) && npc instanceof Monster) || npc.getLifeStats().isAlreadyDead())
				continue;
			float range = (float) MathUtil.getDistance(npc, owner)-getOwner().getBoundingRadius().getCollision()-npc.getBoundingRadius().getCollision();
			if (range > ((CreatureTemplate)getOwner().getObjectTemplate()).getCreatureStatData().getSensoryRange())
				continue;
			if (!GeoEngine.getInstance().canSee(npc, owner))
				continue;
			getOwner().getAggroList().addHate(npc, 1);
			
			if (owner.isInState(CreatureState.COMBAT))
			{
				owner.unsetState(CreatureState.COMBAT);
				owner.setState(CreatureState.WALKING);
				PacketSendUtility.broadcastPacket(owner, new SM_EMOTION(owner, EmotionType.START_EMOTE2, 0, 0));
				PacketSendUtility.broadcastPacket(owner, new SM_EMOTION(owner, EmotionType.NEUTRALMODE, 0, 0));
			}
			return;
		}
		super.thinkActive();
	}
	

	public static void main(String[] args)
	{
		AIClasss.getInstance().addClass("GuardAI", GuardAI.class);
		for (AionObject object : World.getInstance().getAllObjects())
		{
			if (object instanceof Monster)
			{
				((Monster)object).initializeAi();
			}
		}
	}
}
